//
//  ASAvatarGroupMainView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/29.
//

#import "ASAvatarGroupMainView.h"
#import "ASDetailController.h"

static NSString *ASAvatarGroupMainSubCellStr = @"ASAvatarGroupMainSubCell";

@interface ASAvatarGroupMainSubCell : UICollectionViewCell

@property (strong, nonatomic) UIImageView *mainImage;
@property (strong, nonatomic) UIImageView *shadowImage;
@property (strong, nonatomic) UIImageView *iconImage;
@property (strong, nonatomic) UILabel *numLab;

@end

@implementation ASAvatarGroupMainSubCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.mainImage];
        [self.mainImage addSubview:self.shadowImage];
        [self.contentView addSubview:self.numLab];
        [self.contentView addSubview:self.iconImage];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.shadowImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-8);
    }];
    
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.right.mas_equalTo(self.numLab.mas_left).mas_offset(-5);
        make.size.mas_equalTo(CGSizeMake(11, 9));
        make.centerY.mas_equalTo(self.numLab.mas_centerY);
    }];
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [UIImageView new];
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
        _mainImage.layer.masksToBounds = YES;
        _mainImage.layer.cornerRadius = 7;
        _mainImage.backgroundColor = [ASTool backgroundRandomColor];
    }
    return _mainImage;
}

- (UIImageView *)shadowImage{
    if (!_shadowImage) {
        _shadowImage = [UIImageView new];
        _shadowImage.contentMode = UIViewContentModeScaleAspectFill;
        _shadowImage.clipsToBounds = YES;
    }
    return _shadowImage;
}

- (UIImageView *)iconImage{
    if (!_iconImage) {
        _iconImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"picture_praise_small"]];
    }
    return _iconImage;
}

- (UILabel *)numLab{
    if (!_numLab) {
        _numLab = [UILabel new];
        _numLab.font = [UIFont systemFontOfSize:10];
        _numLab.textColor = [UIColor colorWithHexString:@"ffffff"];
    }
    return _numLab;
}

@end

@implementation ASAvatarGroupMainView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.minimumLineSpacing = 5;
        fl.minimumInteritemSpacing = 5;
        fl.sectionInset = UIEdgeInsetsMake(0, 14, 0, 14);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[ASAvatarGroupMainSubCell class] forCellWithReuseIdentifier:ASAvatarGroupMainSubCellStr];
    }
    return _collectionView;
}

- (void)setModel:(ASHomeMainModel *)model{
    _model = model;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.picture_urls.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH - 28 - 10)/3, (SCREEN_WIDTH - 28 - 10)/3);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASAvatarGroupMainSubCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASAvatarGroupMainSubCellStr forIndexPath:indexPath];
    if (self.model.picture_urls.count > indexPath.row) {
        NSString *imageStr = self.model.picture_urls[indexPath.row];
        [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:imageStr]];
        if (indexPath.row == self.model.picture_urls.count - 1) {
            cell.iconImage.hidden = YES;
            cell.numLab.hidden = YES;
            cell.numLab.text = [NSString stringWithFormat:@"%zd",self.model.picture_urls.count];
        }else{
            cell.iconImage.hidden = YES;
            cell.numLab.hidden = YES;
        }
//        if (self.model.lowImageArr.count > indexPath.row) {
//            [cell.mainImage setImage:self.model.lowImageArr[indexPath.row]];
//        }else if(self.model.heightImageArr.count > indexPath.row){
//            [cell.mainImage setImage:self.model.heightImageArr[indexPath.row]];
//        }else{
////            NSString *imageStr = self.model.model.pictureurls[indexPath.row];
////            [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:imageStr]];
//        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.model.picture_urls.count > indexPath.row) {
        NSMutableArray *arr = [NSMutableArray new];
        for (int x = 0; x < self.model.picture_urls.count; x++) {
            ASHomeMainModel *subM = [ASHomeMainModel new];
            subM.tID = self.model.tID;
            subM.avatar = self.model.avatar;
            subM.descriptionStr = self.model.descriptionStr;
            subM.nickname = self.model.nickname;
            subM.userid = self.model.userid;
            subM.is_collect = self.model.is_collect;
            subM.tag_arr = self.model.tag_arr;
            subM.thumb = self.model.picture_urls[x];
            [arr addObject:subM];
        }
        
        ASDetailController *detailVC = [ASDetailController new];
        detailVC.hidesBottomBarWhenPushed = YES;
        [detailVC createOldArr:arr showIndex:indexPath.row];
        [currViewController().navigationController pushViewController:detailVC animated:YES];
    }
}

@end
